﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;

namespace Bank_database_system.业务部
{
    public partial class 交易记录查询b : UserControl
    {
        public event EventHandler ButtonBack;
        DataTable records;
        int pages = 0;
        int pagemax = 0;
        bool j = false;
        交易记录查询c query_records3 = new 交易记录查询c();
        public 交易记录查询b()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            query_records3.ButtonBack += BackToB;
        }

        private void BackToB(object sender, EventArgs e)
        {
            // 修改交易记录查询B中的控件状态      
            panel1.Visible = false;
            panel1.Controls.Clear();
            if (j && TransactionRecord.createInquiry(textBox5.Text, dateTimePicker1.Text, dateTimePicker2.Text, comboBox2.Text, comboBox1.Text, textBox3.Text))
            {
                j = false;
                int i = TransactionRecord.count();
                if (i > 0)
                {
                    pagemax = i / 10 + 1;
                    pages = 1;

                }
                else
                {
                    pagemax = 0;
                    pages = 0;
                }
                updataPages(false);
            }
        }
        public void CardID_Set(string text)
        {
            textBox5.Text = text;
            DateTime today = DateTime.Today;
            dateTimePicker2.Value = today;
            dateTimePicker1.Value = today.AddDays(-7);

            if (TransactionRecord.createInquiry(text, today.AddDays(-7).ToString("yyyy/MM/dd"), "", "", "", ""))
            {
                int i = TransactionRecord.count();
                if (i > 0)
                {
                    pagemax = (i - 1) / 10 + 1;
                    pages = 1;

                }
                else
                {
                    pagemax = 0;
                    pages = 0;
                }
                updataPages(false);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Today;
            dateTimePicker2.Value = today;
            dateTimePicker1.Value = today.AddDays(-7);
            comboBox1.Text = "";
            comboBox2.Text = "";
            textBox3.Text = "";
            textBox5.Text = "";
            pages = 0;
            pagemax = 0;
            label9.Text = pages.ToString();
            label13.Text = pagemax.ToString();
            dataGridView1.DataSource = null;
            TransactionRecord.dropview();
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Today;
            dateTimePicker2.Value = today;
            dateTimePicker1.Value = today.AddDays(-7);
            comboBox1.Text = "";
            comboBox2.Text = "";
            textBox3.Text = "";
            pages = 0;
            pagemax = 0;
            label9.Text = pages.ToString();
            label13.Text = pagemax.ToString();
            dataGridView1.DataSource = null;
            if (TransactionRecord.createInquiry(textBox5.Text, dateTimePicker1.Text, dateTimePicker2.Text, comboBox1.Text, comboBox2.Text, textBox3.Text))
            {
                int i = TransactionRecord.count();
                if (i > 0)
                {
                    pagemax = (i - 1) / 10 + 1;
                    pages = 1;

                }
                else
                {
                    pagemax = 0;
                    pages = 0;
                }
                updataPages(false);
            }
            //调用筛选以重置
            MessageBox.Show("重置已完成!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //筛选作用
            if (TransactionRecord.createInquiry(textBox5.Text, dateTimePicker1.Text, dateTimePicker2.Text, comboBox1.Text, comboBox2.Text, textBox3.Text))
            {
                int i = TransactionRecord.count();
                if (i > 0)
                {
                    pagemax = (i - 1) / 10 + 1;
                    pages = 1;

                }
                else
                {
                    pagemax = 0;
                    pages = 0;
                }
                updataPages(true);
            }
        }
        private void button12_Click(object sender, EventArgs e)
        {
            pages--;
            if (pages < 1)
            {
                pages = 1;
            }
            else
            {
                updataPages(true);
            }
        }
        private void button13_Click(object sender, EventArgs e)
        {
            pages++;
            if (pages > pagemax)
            {
                pages = pagemax;
            }
            else
            {
                updataPages(true);
            }
        }

        private void button9_Click(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {

                //切换到下一页面
                panel1.Visible = true;
                panel1.Controls.Clear();
                DataGridViewRow p = dataGridView1.Rows[e.RowIndex];
                if (p.Cells["STATE"].Value.ToString() == "转账中")
                    j = true;
                query_records3.CardID_Set(p.Cells["CREDIT_CARD_NUMBERS"].Value.ToString(), p.Cells["ASSIST_CARD_NUMBERS"].Value.ToString(), p.Cells["TRANSACTION_ID"].Value.ToString(), p.Cells["TRANSACTION_TIME"].Value.ToString(), p.Cells["TRANSACTION_COMMENT"].Value.ToString(), p.Cells["AMOUNT"].Value.ToString(), p.Cells["STATE"].Value.ToString());
                panel1.Controls.Add(query_records3);
                panel1.BringToFront();//显示置顶
            }
        }
        private void updataPages(bool i)
        {
            label13.Text = pages.ToString();
            label9.Text = pagemax.ToString();
            dataGridView1.DataSource = TransactionRecord.getPage(pages);
            if (dataGridView1.Rows.Count <= 0 && i)
            {
                dataGridView1.DataSource = null;
                MessageBox.Show("查询结果不存在");
            }

        }
    }
}